<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="地址" prop="parkAddress">
        <el-input
          v-model="queryParams.parkAddress"
          placeholder="请输入停车场地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单价" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名字" prop="parkName">
        <el-input
          v-model="queryParams.parkName"
          placeholder="请输入停车场名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="parkingList" @selection-change="handleSelectionChange">
      <el-table-column label="停车场ID" align="center" prop="id" />
      <el-table-column label="停车场地址" align="center" prop="parkAddress" />
      <el-table-column label="单价（元/时）" align="center" prop="price" />
      <el-table-column label="停车场名字" align="center" prop="parkName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button link type="primary" icon="el-icon-car" @click="handleUpdate(scope.row)" v-hasPermi="['creditlife:parking']">
            <i class="el-icon-shop"></i>立即预定停车位
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 预定车位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="parkingRef" :model="form" label-width="100px">
        <el-form-item label="停车场地址" prop="parkAddress">
          <el-tag v-if="form.parkAddress">{{ form.parkAddress }}</el-tag>
        </el-form-item>
        <el-form-item label="单价（元/时）" prop="price">
          <el-tag v-if="form.price">{{ form.price }}</el-tag>
        </el-form-item>
        <el-form-item label="停车场名字" prop="parkName">
          <el-tag v-if="form.parkName">{{ form.parkName }}</el-tag>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">免押金预定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listParking, getParking, delParking, addParking, updateParking } from "@/api/creditlife/parking"

export default {
  name: "Parking",
  data() {
    return {
      parkingList: [],
      open: false,
      loading: true,
      showSearch: true,
      ids: [],
      single: true,
      multiple: true,
      total: 0,
      title: "",
      form: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parkAddress: null,
        price: null,
        parkName: null
      },
      defaultForm: {
        id: null,
        parkAddress: null,
        price: null,
        parkName: null
      },
      rules: {
        parkAddress: [
          { required: true, message: "停车场地址不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "单价不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询parking列表 */
    getList() {
      this.loading = true
      const params = {
        ...this.queryParams,
        // fuzzySearch: true // 添加模糊查询标识
      }
      listParking(this.queryParams).then(response => {
        this.parkingList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // // 表单重置
    // reset() {
    //   this.form = {
    //     id: null,
    //     parkAddress: null,
    //     price: null,
    //     parkName: null
    //   }
    //   this.$refs.parkingRef.resetFields()
    // },
    // 表单重置
    reset() {
      // 重置数据
      this.form = Object.assign({}, this.defaultForm)

      // 延迟执行表单重置，确保DOM已更新
      this.$nextTick(() => {
        if (this.$refs.parkingRef) {
          this.$refs.parkingRef.clearValidate()
        }
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryRef.resetFields()
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getParking(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "预定车位"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.parkingRef.validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateParking(this.form).then(response => {
              this.$modal.msgSuccess("预定成功")
              this.open = false
              this.getList()
            })
          } else {
            addParking(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除parking编号为"' + ids + '"的数据项？').then(function() {
        return delParking(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('creditlife/parking/export', {
        ...this.queryParams
      }, `parking_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
